package com.bm.console.business.impl;

import com.aliyun.oss.OSSClient;
import com.bm.console.business.UploadService;
import com.bm.console.common.OSSConfig;
import com.sys.object.common.utils.OSSUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen365 on 2018/04/03.
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Override
    public String upload(File file) throws Exception {
        try {
            OSSClient ossClient = new OSSClient(OSSConfig.oss_endpoint, OSSConfig.oss_accessKeyId, OSSConfig.oss_accessKeySecret);
            String md5key = OSSUtil.uploadObject2OSS(ossClient, file, OSSConfig.oss_backetName,"photo/");
            FileUtils.deleteQuietly(file);
            String imageUrl = file.getName();
            // 返回"图像信息"选项卡并显示图片 ,在对应的文本框中显示图片资源url
            if (!StringUtils.isEmpty(imageUrl)) {
                return OSSConfig.oss_domain +"photo/"+ imageUrl;
            }
            ossClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
